package com.gomore.opple.module;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatDelegate;
import android.view.Window;

import com.gomore.opple.OppleApplication;
import com.gomore.opple.R;
import com.gomore.opple.data.DataRepositoryComponent;
import com.gomore.opple.utils.AnimationUtils;
import com.gomore.opple.widgets.ActionBarTitle;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;

public abstract class BaseActivity extends FragmentActivity implements ActionBarTitle.ActionBarTitleClickListener {
    protected ActionBarTitle mActionBarTitle;
    protected FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        fragmentManager = getFragmentManager();

        showActivityInAnim();
        setContentView(getLayoutView());
        ButterKnife.bind(this);
        initalizeViewData();
        initActionBar();
        initalizeViews();
        initializeDependencyInjector();
    }

    protected void initalizeViewData() {
    }

    /**
     * 初始化视图
     */
    protected abstract void initalizeViews();

    /**
     * 初始化注入
     */
    protected abstract void initializeDependencyInjector();

    /**
     * 进入动画
     */
    protected void showActivityInAnim() {
        AnimationUtils.showActivityInAnim(this);
    }

    protected DataRepositoryComponent getRepositoryComponent() {
        return OppleApplication.getInstance().getRepositoryComponent();
    }

    public void initActionBar() {
        mActionBarTitle = (ActionBarTitle) findViewById(R.id.abt);
        if (mActionBarTitle != null) {
            mActionBarTitle.setClickListener(this);
        }
    }


    /**
     * 左侧图标点击事件
     */
    @Override
    public void onLeftIconClick() {
        finish();
        AnimationUtils.showActivityExitAnim(this);
    }

    /**
     * 右侧图标点击事件
     */
    @Override
    public void onRightIconClick() {
    }

    /**
     * 布局资源
     *
     * @return
     */
    protected abstract int getLayoutView();

    /**
     * @param fragment
     * @param isAddToBackStack
     * @param tag
     */
    protected void replaceFragment(Fragment fragment, boolean isAddToBackStack, String tag) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.contentFrame, fragment, tag);
        if (isAddToBackStack) {
            fragmentTransaction.addToBackStack(tag);
        }
        fragmentTransaction.commit();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
